package com.samoilovich.belarustoday.adapters;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.datemodel.CategoryModel;
import com.samoilovich.belarustoday.datemodel.NewsModel;
import com.samoilovich.belarustoday.tools.Utility;

import java.util.ArrayList;

public class CategoryItemsAdapter extends BaseAdapter implements ImageLoadingListener {
    private Activity mContext;
    private ArrayList<NewsModel> mNews;
    private int mLayoutRes;
    private CategoryModel mCurrentCategory;
    private boolean mPictureLoadingFlag;

    public CategoryItemsAdapter(Activity context, ArrayList<NewsModel> news, int layoutRes, CategoryModel category) {
        mContext = context;
        mNews = news;
        mPictureLoadingFlag = Utility.getPictureLoadingState(mContext);
        mLayoutRes = layoutRes;
        mCurrentCategory = category;
    }

    public int getCount() {
        if (mNews != null) {
            return mNews.size();
        }

        return 0;
    }

    public NewsModel getItem(int position) {
        if (mNews != null && mNews.size() > position) {
            return mNews.get(position);
        }

        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        NewsModel currentItem;
        Integer pos;

        pos = Integer.valueOf(position);
        currentItem = mNews.get(position);
        if (convertView == null) {
            convertView = mContext.getLayoutInflater().inflate(mLayoutRes, null);
            holder = new ViewHolder();
            holder.LogoWait = (ProgressBar) convertView.findViewById(R.id.progressBar_waitLogo);
            holder.Logo = (ImageView) convertView.findViewById(R.id.imageView_logo);
            holder.Title = (TextView) convertView.findViewById(R.id.textView_categoryContentTitle);
            holder.Details = (ImageButton) convertView.findViewById(R.id.imageButton_details);
            holder.Subtitle = (TextView) convertView.findViewById(R.id.textView_date);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.LogoWait.setTag(pos);
        holder.Logo.setTag(holder.LogoWait);
        if (currentItem.getImageLink() != null && !currentItem.getImageLink().equals(Constants.EMPTY_STRING)) {
            if (currentItem.getImageDrawable() != null && currentItem.getImageDrawable().get() != null) {
                holder.Logo.setVisibility(View.VISIBLE);
                holder.Logo.setScaleType(ImageView.ScaleType.CENTER_CROP);
                holder.LogoWait.setVisibility(View.GONE);
                holder.Logo.setImageDrawable(currentItem.getImageDrawable().get());
            } else {
                if (mPictureLoadingFlag) {
                    holder.Logo.setVisibility(View.GONE);
                    holder.LogoWait.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().denyNetworkDownloads(false);
                    ImageLoader.getInstance().displayImage(currentItem.getImageLink(), holder.Logo, this);
                } else {
                    holder.Logo.setVisibility(View.GONE);
                    holder.LogoWait.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().denyNetworkDownloads(true);
                    ImageLoader.getInstance().displayImage(currentItem.getImageLink(), holder.Logo, this);
                }
            }
        } else {
            if (mCurrentCategory.getImageDrawable() != null) {
                holder.Logo.setImageDrawable(mCurrentCategory.getImageDrawable());
            } else {
                holder.Logo.setImageResource(R.drawable.category_no_image);
            }
            holder.Logo.setVisibility(View.VISIBLE);
            holder.Logo.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            holder.LogoWait.setVisibility(View.GONE);
        }
        holder.Title.setText(currentItem.getTitle());
        holder.Details.setTag(pos);
        holder.Subtitle.setText(currentItem.getTime());
        convertView.setTag(holder);

        return convertView;
    }

    public void setPictureLoadingState(boolean state) {
        mPictureLoadingFlag = state;
    }

    public void setNews(ArrayList<NewsModel> news) {
        mNews = news;
        notifyDataSetChanged();
    }

    @Override
    public void onLoadingStarted(String imageUri, View view) {
        view.setVisibility(View.GONE);
        ((View) view.getTag()).setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
        ImageView image = (ImageView) view;

        if (mCurrentCategory.getImageDrawable() == null) {
            image.setImageResource(R.drawable.category_no_image);
        } else {
            image.setImageDrawable(mCurrentCategory.getImageDrawable());
        }

        image.setVisibility(View.VISIBLE);
        image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        ((View) view.getTag()).setVisibility(View.GONE);
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        ImageView image = (ImageView) view;
        View tag = (View) view.getTag();
        Integer position = ((Integer) tag.getTag());

        tag.setVisibility(View.GONE);
        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if (image.getDrawable() == null) {
            if (mCurrentCategory.getImageDrawable() == null) {
                image.setImageResource(R.drawable.category_no_image);
            } else {
                image.setImageDrawable(mCurrentCategory.getImageDrawable());
            }
        } else {
            mNews.get(position).setImageDrawable(image.getDrawable());
        }
        image.setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {
    }

    // -----------------------------------------------------------------------------------
    private class ViewHolder {
        ProgressBar LogoWait;
        ImageView Logo;
        TextView Title;
        TextView Subtitle;
        ImageButton Details;
    }
}
